package org.example.jiaxiao.controller;

import org.example.jiaxiao.entity.Account;
import org.example.jiaxiao.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class LoginController {
    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private LoginService loginService;

    @GetMapping("/test")
    public ResponseEntity<?> test() {
        logger.info("Test endpoint called");
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "服务正常运行");
        return ResponseEntity.ok(response);
    }

    @PostMapping("/login")
    public ResponseEntity<?> login(@RequestBody Map<String, String> loginRequest) {
        logger.info("Login request received: {}", loginRequest);
        
        if (loginRequest == null) {
            logger.error("Login request is null");
            Map<String, Object> response = new HashMap<>();
            response.put("code", 400);
            response.put("message", "请求数据不能为空");
            return ResponseEntity.ok(response);
        }

        String username = loginRequest.get("username");
        String password = loginRequest.get("password");
        String userType = loginRequest.get("userType");

        if (username == null || password == null || userType == null) {
            logger.error("Missing required fields - username: {}, password: {}, userType: {}", 
                username != null, password != null, userType != null);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 400);
            response.put("message", "用户名、密码和用户类型不能为空");
            return ResponseEntity.ok(response);
        }

        logger.info("Login attempt - username: {}, userType: {}", username, userType);

        try {
            Account account = loginService.login(username, password, userType);
            if (account != null) {
                logger.info("Login successful for user: {}", username);
                Map<String, Object> response = new HashMap<>();
                response.put("code", 200);
                response.put("message", "登录成功");
                response.put("data", account);
                return ResponseEntity.ok(response);
            } else {
                logger.warn("Login failed - Invalid credentials for user: {}", username);
                Map<String, Object> response = new HashMap<>();
                response.put("code", 401);
                response.put("message", "用户名或密码错误");
                return ResponseEntity.ok(response);
            }
        } catch (Exception e) {
            logger.error("Login error for user: " + username, e);
            throw e; // 让全局异常处理器处理
        }
    }
} 